package org.vectrics.skill;

import java.util.HashSet;
import java.util.Iterator;

import org.apache.log4j.Category;




abstract public class SkillSet extends HashSet implements java.io.Serializable  {
    private static Category log = Category.getInstance(SkillSet.class);
    private Skill rootSkill = null;


	protected abstract Skill createSkill();
    public SkillSet() {
        rootSkill = createSkill();
        rootSkill.setDescription("Major Categories");
        rootSkill.setCode("root");
        rootSkill.setSkillParent(null);
        add(rootSkill);
    }

    public Skill getRootSkill()
    {
        return(rootSkill);
    };

    public void add(Skill iSkill) {
        this.add((Object) iSkill);
        log.debug("Skill: " + iSkill.getDescription() + " -- added to SkillSet");

        if (iSkill != rootSkill) {

            Skill parent = iSkill.getSkillParent();
            if (parent == null)
                log.error("Parent not found for skill: " 
                		+ iSkill.getDescription() + " ParentCode = " 
						+ iSkill.getSkillParent().getCode());
            else
                parent.getChildren().add(iSkill);
        }
    };

    public Skill find(String iSkillCode)
    {
        Skill resultSkill = null;
        Iterator iter = this.iterator();
        while ((iter.hasNext()) && (resultSkill == null)) {
            Skill skill = (Skill)iter.next();
            if (skill.getCode().compareTo(iSkillCode.trim()) == 0) {
                resultSkill = skill;
            };
        };

        if (resultSkill == null)
            log.warn("find() didn't find skill for code: " + iSkillCode);

        return(resultSkill);
    };


    public String getHierarchy() {
        return(getRootSkill().getHierarchy());
    }

    
}